


import configparser
import csv
import os.path

import openpyxl

# 获取项目文件路径
def get_protectpath():
    # __file__是python的内置属性，能获取当前模块的路径
    fpath = os.path.dirname(__file__)
    protectpath = os.path.dirname(fpath)
    return protectpath

#读取表格文件
def readxlsx(filepath,sheetname='sheet1'):


    # 获取完整的表格路径
    path=get_protectpath()+'\\'+filepath
    # 读取表格文件
    wb = openpyxl.load_workbook(path)
    # 获取某个工资表内容
    wsh = wb[sheetname]

    # 创建返回的空列表
    testls =[]

    # 遍历获取内容
    for row in wsh:
        #创建空的子列表
        cells = []
        for cell in row:
            #获取单元格内容
            celltext = cell.value
            # 判断如果单元格为空，则存储空字符串
            if  celltext is None:
                cells.append('')
            else:
                # 将单元格内容存储在子列表中
                cells.append(celltext)
        # 将子列表添加到返回的空列表中
        testls.append(cells)

    return testls[1::]

# 读取ini文件
def readini(filepath,section,key):
    # 获取完整的文件路径
    path =get_protectpath() + '\\' + filepath
    # path = os.path.join(get_protectpath(), filepath)
    cp =configparser.ConfigParser()
    cp.read(path,encoding='utf-8')
    value=cp.get(section,key)
    return value

#读取csv文件
def readcsv(filepath,key,value):
    results =[]
    with open(filepath,mode='r',encoding='utf-8') as file:
        csv_reader =csv.DictReader(file)
        if key not in csv_reader.fieldnames:
            print(f"警告: 键 '{key}' 不存在于CSV文件的表头中")
            return results
        for row in csv_reader:
            if row[key] == str(value):
                results.append(row)
    return results

if __name__ == '__main__':
    ls = readxlsx("datas\\data_addproduct.xlsx","Sheet1")
    print(ls)

    v = readini("datas\\env.ini", "env", "url")
    print(v)



